package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.Admin;
import com.itheima.mapper.AdminMapper;
import com.itheima.service.db.AdminService;
import com.itheima.util.HttpUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 查询到的用户
     */
    @Override
    public Admin findAdminByName(String username) {

        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return adminMapper.selectOne(wrapper);
    }

    /**
     * 根据id查询用户信息
     * @param id 用户id
     * @return 用户对象
     */
    @Override
    public Admin finById(Long id) {
        return adminMapper.selectById(id);
    }


    @Value("${wx.appid}")
    private String appid;
    @Value("${wx.secret}")
    private String secret;
    @Value("${wx.accessTokenUrl}")
    private String accessTokenUrl;
    @Value("${wx.wxInfoUrl}")
    private String wxInfoUrl;
    /**
     * 微信扫码登录
     * @param code 临时令牌
     * @return
     */
    @Override
    public Admin wxLogin(String code) {
        Admin admin = null;
        //1.根据code获取access_token和openId
        String atUtl = accessTokenUrl + "?code="+code+"&appid="+appid+"&secret="+secret+"&grant_type=authorization_code";
        System.out.println(atUtl);
        Map<String, Object> map1 = HttpUtils.sendGet(atUtl);
        Object access_token = map1.get("access_token");
        System.out.println(access_token);
        Object openid = map1.get("openid").toString();
        System.out.println(openid);
        if(access_token == null && openid == null) {
            return admin;
        }

        //2.根据openId判断用户是否存在

        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("open_id", openid.toString());
        admin = adminMapper.selectOne(wrapper);
        if(admin != null) {
            System.out.println("返回数据库中的用户对象");
            //3.如果用户存在返回用户信息
            return admin;
        }else{
            System.out.println("查询微信数据库");
            //4.如果用户不存在，根据access_token和openId获取微信用户信息
            String wxurl = wxInfoUrl + "?access_token=" + access_token +"&openid="+openid;
            Map<String, Object> map2 = HttpUtils.sendGet(wxurl);
            Object nickname = map2.get("nickname");
            Object headimgurl = map2.get("headimgurl");
            if(nickname == null || headimgurl == null) {
                return admin;
            }
            //5.将微信用户信息保存到数据库，返回用户数据
            admin = new Admin();

            admin.setCreated(new Date());//注册日期
            admin.setUpdated(new Date());//最后修改日期
            admin.setOpenId(openid.toString());//这只openId
            admin.setUsername(nickname.toString());
            admin.setPassword("123456");
            adminMapper.insert(admin);
        }
        return admin;
    }
}
